<template>
    <div class="home" ref="homeRef">
        <van-nav-bar title="待接收卡" left-text="" left-arrow @click-left="onClickLeft" :fixed="true" :placeholder="true" />
        <div class="tabs">
            <van-tabs v-model:active="active" @click-tab="changeTabs" :swipeable="true" :ellipsis="false" shrink>
                <van-tab :title="item.name" v-for="item in typeList" :name="item.id"></van-tab>
                <!-- <van-tab title="一次性卡"></van-tab>
            <van-tab title="其他卡"></van-tab> -->
            </van-tabs>
            <div class="sumTitle" v-if="list.length!=0">
                <title-item title1st="总计" :subTitle="tolal"> </title-item>
            </div>
        </div>
        <div class="content">
            <div v-for="(i, index) in list" :key="index">
                <div class="item">
                    <div class="left">
                        <van-icon :name="getAssetURL('order/cardNum.png')" color="#2CB9A4" size="24" class="mar-rig" />
                        <text class="useFunc">卡号：{{ i.exchange_number }}</text>
                    </div>
                    <div class="right">
                        <span class="mar-rig">卡密：******</span>
                    </div>
                </div>
                <BorderDashed />
            </div>
            <noData :list="list" :lastPage="last_page" :page="page" />
        </div>
        <div class="botBtn" v-if="list.length!=0">
            <div style=" width: 94%;margin: 0 auto; ">
                <!-- <u-button class="custom-style" text="提现"
					customStyle=" background:linear-gradient(90deg, #218CC9 0%, #2CB9A4 100%);color:#fff;margin-top:50px;border-radius: 30px;"
					@click="goAuth">
				</u-button> -->
                <van-button color="linear-gradient(90deg, #218CC9 0%, #2CB9A4 100%)" block round="true"
                    @click="receiveCard('1')">
                    接受
                </van-button>
                <van-button style="margin-top: 10px;" color="#FF6666" plain hairline block round="true"
                    @click="receiveCard('2')">
                    拒绝
                </van-button>
            </div>
        </div>
    </div>
</template>
<script setup>
import {
    showToast
} from 'vant';
import { getAssetURL } from "@/utils/load_assets.js"
import BorderDashed from '@/components/border-dashed/border-dashed.vue'
import TitleItem from "@/components/title-item/title-item.vue";
import { useRouter } from 'vue-router'
import useScroll from '@/hooks/useScroll'
import noData from "@/components/noData/noData.vue"
import { getEntityName, entityList, receiveEntity } from "@/services/modules/card";
const router = useRouter();
import { ref, watch } from 'vue';
import { onMounted } from "vue";
const active = ref(0);
const list = ref([])
const last_page = ref(1)
const page = ref(1)
const typeList = ref([])
const typeId = ref('')
const tolal = ref('')
// 返回按钮
const onClickLeft = () => {
    router.back();
};
const changeTabs = (e) => {
    console.log(e)
    typeId.value = e.name//将id赋值
    list.value = []//清空数组
    page.value = 1
    getList()
}
const getName = async () => {
    let res = await getEntityName({ type: '1' })
    typeList.value = res.data
    list.value = []//清空数组
    if(typeList.value.length === 0){
        return;
    }
    typeId.value = typeList.value[0].id
    if (typeList.value.length && typeList.value.length != 0) {
        getList()
    }
    // console.log(typeList.value,'getName')
}
const receiveCard = async (status) => {
    let res = await receiveEntity({ status ,id:typeId.value})
    // console.log(res)
    showToast(res.msg)
    setTimeout(()=>{
        getName()
    },1000)
}
// 监听滚动到底部
const homeRef = ref()
const { isReachBottom, scrollTop } = useScroll(homeRef)
// console.log(homeRef.value)
watch(isReachBottom, (newValue) => {
    // console.log(newValue)
    if (newValue) {
        if (page.value == last_page.value) {
            return
        } else {
            page.value++
            getList()
        }
    }
})
//获取列表
const getList = async () => {
    let res = await entityList({ page: page.value, type: '1', id: typeId.value })
    list.value = [...list.value, ...res.data.data]
    // list.value = [...list.value, ...res.data.data]
    // list.value = [...list.value, ...res.data.data]
    tolal.value = res.data.total
    // console.log(tolal,222)
    last_page.value = res.data.last_page
    // console.log(list.value, last_page.value)
}
onMounted(() => {
    getName()
})
</script>
<style lang="scss" scoped>
::v-deep .van-tabs__line {
    background: #218CC9 !important;
}

::v-deep .van-tab {
    // justify-content: flex-start !important;
}

::v-deep .van-tabs {
    // padding-right: 80px;
}

::v-deep .van-tab--active {
    color: #2CB9A4 !important;
}

.home {
    background: #F6F6F6;
    height: 100vh;
    overflow-y: auto;
    box-sizing: border-box;
}

.tabs {
    position: sticky !important;
    top: 46px !important;
    z-index: 10 !important;
    background: #fff;

    .sumTitle {
        margin-left: -4px;
        margin-top: 10px;
        padding-right: 20px;
    }
}

.botBtn {
    position: fixed;
    width: 100%;
    bottom: 10px;
}

.content {
    padding: 20px;

    .item {
        display: flex;
        align-items: center;
        justify-content: space-between;

        .left {
            color: #1D1B20;
            font-family: PingFang SC;
            font-size: 14px;
            font-style: normal;
            font-weight: 400;
            line-height: normal;
            display: flex;
            align-items: center;
        }

        .right {
            color: #1D1B20;

            &:first-child {
                font-family: PingFang SC;
                font-size: 14px;
                font-style: normal;
                font-weight: 400;
                line-height: normal;
            }

            &:last-child {
                font-family: PingFang SC;
                font-size: 14px;
                font-style: normal;
                font-weight: 400;
                line-height: normal;
            }
        }

        .mar-rig {
            margin-right: 10px;
        }
    }
}</style>